﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RedSand.Site.Models;
using RedSand.Site.Controllers;
using RedSand.Web.Mvc;

namespace Site.Controllers
{
    public class CategoryController : BaseController
    {
        public ActionResult Manager(byte type)
        {
            if (type != 1 && type != 2) return RedirectToAction("Manager", new { type = 1 });
            ManagerModel model = new ManagerModel();
            model.Title = type == 1 ? Translate("NewsCategory", "News") : Translate("GuideCategory", "Guide");
            return View(model);
        }
        [AjaxOrChildAction]
        public ActionResult ListCategory(string Title, int Type)
        {
            ViewBag.Type = Type;
            ViewBag.Title = Title;
            return PartialView(RsDataManager.GetList<CategoryInfo>("CategorySearch", new { Type = Type, Language = LanguageCode }).OrderBy(model => model.Order).ToList());
        }
        

        [AjaxOrChildAction]
        public ActionResult List(byte type) 
        {
            ViewBag.Type = type;
            ViewBag.Title = type == 1 ? Translate("NewsCategory", "News") : Translate("GuideCategory", "Guide");
            return PartialView(RsDataManager.GetList<CategoryInfo>("CategorySearch", new { Type = type, Language = LanguageCode }));
        }

        [AjaxOrChildAction]
        public MvcHtmlString GetSelectList(byte type, byte? status, bool? validate, string name, int? selectedValue)
        {
            List<CategoryInfo> list = RsDataManager.GetList<CategoryInfo>("CategorySearch", new { Type = type, Language = LanguageCode, Status = status });
            TagBuilder select = new TagBuilder("select");
            select.Attributes["name"] = name ?? "CategoryID";
            select.GenerateId(name ?? "CategoryID");
            if (validate.HasValue && validate.Value)
            {
                select.Attributes["data-val"] = "true";
                select.Attributes["data-val-required"] = "The " + (name ?? "Category") + " field is required.";
            }
            if (list != null)
            {
                TagBuilder option = new TagBuilder("option");
                select.InnerHtml += option.ToString();
                foreach (var item in list)
                {
                    if (selectedValue.HasValue && item.CategoryID == selectedValue.Value)
                    {
                        option.Attributes["selected"] = "selected";
                    }
                    else
                    {
                        option.Attributes.Remove("selected");
                    }

                    option.Attributes["value"] = item.CategoryID.ToString();
                    option.SetInnerText(item.Title);
                    select.InnerHtml += option.ToString();
                }
            }
            return MvcHtmlString.Create(select.ToString());
        }

        [AjaxOrChildAction]
        public MvcHtmlString GetCategoryName(int id)
        {
            CategoryInfo info = RsDataManager.GetInfo<CategoryInfo>("CategoryGetInfo", "CategoryID", id);
            return MvcHtmlString.Create(info == null ? "" : info.Title);
        }

        [HttpGet, AjaxOrChildAction]
        public ActionResult Create(byte type, int count)
        {
            return PartialView(new CategoryInfo { Type = type, Order = count + 1 });
        }

        [HttpPost, AjaxOrChildAction]
        public ActionResult Create(CategoryInfo info)
        {
            if (ModelState.IsValid)
            {
                info.Language = LanguageCode;
                if (RsDataManager.Save("CategorySave", info))
                {
                    AddMessage(Message.CreateSuccess);
                    return RedirectToAction("List", new { type = info.Type });
                }
                return AjaxMessage(Message.CreateError);
            }
            return AjaxModelError();
        }


        [HttpGet, AjaxOrChildAction]
        public ActionResult Edit(int id, int count)
        {
            ViewBag.ChildCount = count;
            return PartialView(RsDataManager.GetInfo<CategoryInfo>("CategoryGetInfo", "CategoryID", id));
        }

        [HttpPost, AjaxOrChildAction]
        public ActionResult Edit(CategoryInfo info)
        {
            if (ModelState.IsValid)
            {
                if (RsDataManager.Save("CategorySave", info))
                {
                    AddMessage(Message.UpdateSuccess);
                    return RedirectToAction("List", new { type = info.Type });
                }
                return AjaxMessage(Message.UpdateError);
            }
            return AjaxModelError();
        }

        [AjaxOrChildAction]
        public ActionResult Delete(int id, byte? type)
        {
            if (RsDataManager.Delete("CategoryDelete", "CategoryID", id))
            {
                AddMessage(Message.DeleteSuccess);
                return RedirectToAction("List", new { type = type });
            }
            return AjaxMessage(Message.DeleteError);
        }
    }
}
